/**
  * AntRanking Copyright (c) 2008-2012 INRIA, University of Lille 1
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2 of the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  * 
  * Contact: 
  * 	benjamin.bertein@gmail.com,
  * 	nacim.hamdad@gmail.com
  * 	nicolas.haderer@inria.fr
  *
  * Author: Nacim Hamdad,Benjamin Bertein, Haderer Nicolas
  */
package com.google.antranking.activity;

import com.google.antranking.AbstractAntRankingActivity;
import com.google.antranking.AntRankingActivity;
import com.google.antranking.R;
import com.google.antranking.utils.AtConfiguration;
import com.google.antranking.utils.AtConstant;

import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;


public class SettingActivity extends AbstractAntRankingActivity implements OnClickListener{

	private EditText edServer;
	
	private EditText edUsername;
	
	private EditText edPassword;
	
	private CheckBox cbLogError;
	
	private CheckBox cbLogInfo;
	
	private Button btSave;
	
	private Button btCancel;
	
	public void onAtCreate(Bundle savedInstanceState) {
		setContentView(R.layout.layout_setting);
	
		this.edServer = (EditText) this.findViewById(R.id.layout_setting_ed_server);
		this.edServer.setText(AtConfiguration.getStringValue(this,AtConstant.PREF_HOST,"null"));
		
		this.edUsername = (EditText) this.findViewById(R.id.layout_setting_ed_username);
		this.edUsername.setText(AtConfiguration.getStringValue(this,AtConstant.PREF_USERNAME,"null"));
		
		this.edPassword = (EditText) this.findViewById(R.id.layout_setting_ed_password);
		this.edPassword.setText(AtConfiguration.getStringValue(this,AtConstant.PREF_PASSWORD,"null"));
		
		this.cbLogInfo = (CheckBox) this.findViewById(R.id.layout_setting_cb_loginfo);
		this.cbLogInfo.setChecked(AtConfiguration.getBooleanValue(this,AtConstant.PREF_NOTIFICATION_INFO, false));
		
		this.cbLogError = (CheckBox) this.findViewById(R.id.layout_setting_cb_logerror);
		this.cbLogError.setChecked(AtConfiguration.getBooleanValue(this,AtConstant.PREF_NOTIFICATION_ALERT, false));
		
		((Button) this.findViewById(R.id.layout_setting_bt_save)).setOnClickListener(this);
		((Button) this.findViewById(R.id.layout_setting_bt_cancel)).setOnClickListener(this);
		
	}

	@Override
	public void onClick(View v) {
		
		final Button button = (Button) v;
		if (button.getText().equals("Save")){
			this.savePreference();
		}
		else if(button.getText().equals("Cancel")){
			// Do Nothing
		}
		
		final Intent intent = new Intent(this,AntRankingActivity.class);
		startActivity(intent);
	}

	
	private void savePreference(){
		final Editor editor = AtConfiguration.getEditorConfiguration(this);
		editor.putString(AtConstant.PREF_HOST,this.edServer.getText().toString());
		editor.putString(AtConstant.PREF_USERNAME,this.edUsername.getText().toString());
		editor.putString(AtConstant.PREF_PASSWORD,this.edPassword.getText().toString());
		editor.putBoolean(AtConstant.PREF_NOTIFICATION_INFO, this.cbLogInfo.isChecked());
		editor.putBoolean(AtConstant.PREF_NOTIFICATION_ALERT, this.cbLogError.isChecked());
		editor.commit();
	}
	
}
